package com.universalInterface.lims_interface.controller;


import cn.hutool.core.bean.BeanUtil;
import com.universalInterface.common.result.ApiResult;
import com.universalInterface.lims_interface.entity.TestInfo;
import com.universalInterface.lims_interface.service.ITestInfoService;
import com.universalInterface.lims_interface.vo.testinfo.ListTestInfoVo;
import com.universalInterface.lims_interface.vo.testinfo.TestInfoUpdateVo;
import com.universalInterface.lims_interface.vo.testinfo.TestInfoVo;
import com.universalInterface.lims_interface.vo.testinfo.param.ListTestInfoParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 试验信息
 * </p>
 *
 * @author author
 * @since 2023-12-19
 */
@Tag(name = "试验信息")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/testInfo")
public class TestInfoController {

    public final ITestInfoService iTestInfoService;

    @Operation(summary = "新增试验信息")
    @PostMapping("/addtest")
    public ApiResult add(@Parameter(name = "TestInfoEntity", description = "新增试验信息") @Valid @RequestBody TestInfoVo testInfoVo) {
        iTestInfoService.add(BeanUtil.copyProperties(testInfoVo, TestInfo.class));
        return ApiResult.success("试验新增成功");
    }

    @Operation(summary = "查询多个试验信息")
    @PostMapping("/list")
    public ApiResult list(@Parameter(description = "新增试验信息") @Valid @RequestBody ListTestInfoParam param) {
        List<TestInfo> list = iTestInfoService.list(BeanUtil.copyProperties(param, TestInfo.class));
        return ApiResult.success("查询成功！", BeanUtil.copyToList(list, ListTestInfoVo.class));
    }


    @Operation(summary = "清空实时数据表")
    @DeleteMapping("/trucktable")
    public ApiResult cleartable(@Parameter(name = "labNum", description = "实验室编号")  @RequestParam String labNum) {
        iTestInfoService.clearTable(labNum);
        return ApiResult.success("数据清除成功");
    }

    @Operation(summary = "查询该实验室最新的试验")
    @GetMapping("/newestReport")
    public ApiResult newestInfo(@Parameter(name = "labNum", description = "实验室编号") @RequestParam Long labNum) {
       TestInfo testInfos = iTestInfoService.newestInfos(labNum);
        return ApiResult.success("查询成功",testInfos);
    }

    @Operation(summary = "通过id更新试验信息")
    @GetMapping("/updateById")
    public ApiResult updateById(@Parameter(description = "试验信息更新对象") @Valid @RequestBody TestInfoUpdateVo updateVo) {
        Integer i = iTestInfoService.updateById(BeanUtil.copyProperties(updateVo, TestInfo.class));
        return ApiResult.success("更新成功！size = " + i);
    }

}
